/*
 * @Descripttion: 
 * @version: 
 * @Author: by
 * @Date: 2021-05-24 15:14:42
 * @LastEditors: by
 * @LastEditTime: 2021-10-11 16:14:33
 */
import adminApi from '@/api/login'
// import LocalStorage from '@/utils/LocalStorage.js'
import router from '@/router'

export default {
    state: {
        name: '',
        isAdmin: 0,
        token: '',
        userInfo: {},
        permissions: []
    },
    mutations: {
        logOut() {
            localStorage.clear()
            router.push("/login")
        },
        setToken(state, str) {
            state.token = str
            localStorage.setItem("token", str)
        },
        setUserInfo(state, obj) {
            state.userInfo = obj
            localStorage.setItem("userInfo", obj)
        },
        setPermissions(state, arr) {
            state.permissions = arr
            localStorage.setItem("permissions", arr)
        }
    },
    actions: {
        async loginByPwd({ commit }, params) {
            return new Promise((resolve, reject) => {
                adminApi.login(params)
                    .then((res) => {
                        commit('setToken',res.data.accessToken)
                        commit('setUserInfo',JSON.stringify(res.data))
                        commit('setPermissions', JSON.stringify(res.data.perms))
                        resolve(res)
                    })
                    .catch((error) => {
                        reject(error)
                    })
            })
        },
        Logout({ commit }) {
            return new Promise((resolve, reject) => {
                adminApi.logout()
                    .then(() => {
                        commit('logOut')
                    })
                    .catch((error) => {
                        reject(error)
                    })
            })
        },
        loginByYzm({ commit }, params) {
            return new Promise((resolve, reject) => {
                adminApi.loginByyzm(params)
                    .then((res) => {
                        commit('setToken',res.data.accessToken)
                        commit('setUserInfo',JSON.stringify(res.data))
                        resolve(res)
                    })
                    .catch((error) => {
                        reject(error)
                    })
            })
        }
    },
    modules: {}
}